/*
* $Id: data.h,v 1.47 2008/01/03 06:38:50 sunny Exp $
*/
/*******************************************************************************

       UBEC (Uniband Electronic Corp.)

       Project: U-NET01, Ubiquitous network platform

       File: data.h

       Version: 0.3.0

       Usage: Define unet information base (UIB)

       Platform: U-NET01 DK with Keil 8051 C compiler

       Reference:

               Silicon Laboratories: C8051F124, C8051F340

               UBEC: UZ2400, UZ2410

       Note :

               Copyright (C) 2007 Uniband Electronic Corporation, All rights reserved

********************************************************************************/

#ifndef _DATA_H_
#define _DATA_H_


//******************************************************************************
// tinysec data types ,modified by jason 2008/5/20 5:35pm
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef INT16 int16_t;
typedef uint8_t result_t  ;
typedef unsigned int uint32_t;
//**************************************************************************






// Interrupt Structure
typedef struct _interrupt_flag_{
    UINT8 TxN    : 1;
    UINT8 TxG1   : 1;
    UINT8 TxG2   : 1;
    UINT8 Rx     : 1;
    UINT8 Sec    : 1;
    UINT8 Timer  : 1;
    UINT8 Wakeup : 1;
    UINT8 Sleep  : 1;
}INTERRUPT_STS;

typedef struct _unet_system_flag_{
	UINT8 JoinNetwork:1;
	UINT8 RecvAssoRsp:1;
	UINT8 AssoSuccess:1;
	UINT8 PanCoordinator:1;
	UINT8 Router:1;
	UINT8 Endev:1;
	UINT8 FindNetwork:1;
	UINT8 ChildFull:1;

	UINT8 GP0:1; // For general purpose
	UINT8 PermitJoin:1;
	UINT8 Kick:1;
	UINT8 TxFailure:1;
	UINT8 SkipBcnReq:1;
	UINT8 NwkReady:1;
	UINT8 ProcessAssociate:1;
	UINT8 PanIdConflict:1;

	UINT8 NwkReset:1;
	UINT8 Broadcast:1;
	UINT8 AutoChannel:1;	// 2007/11/6 13:21, Sunny: auto select RF channel
	UINT8 AutoPID:1;	// 2007/11/6 13:21, Sunny: auto select PAN ID
}UNET_SF;

#if USE_BEACON_LIST
struct _beacon_list_{
	struct _beacon_list_ *Next;
	UINT16 PID;		// Pan ID
	UINT16 Addr;		// Network Address
	UINT8 Channel;
	UINT16 SfSpec;		// Super frame spec
	UINT8 LinkQuality;
	UINT8 DevDep;		// Device depth
	UINT8 ChildCnt;		// Children count
};

#else
typedef struct _beacon_{
	UINT16 PID;		// Pan ID
	UINT16 Addr;		// Network Address
	UINT8 Channel;
	UINT16 SfSpec;		// Super frame spec
	UINT8 LinkQuality;
	UINT8 DevDep;		// Device depth
	UINT8 ChildCnt;		// Children count
}BEACON;
#endif // #if USE_BEACON_LIST

#if USE_DENIER_LIST
typedef struct denier_list_s {
	struct denier_list_s *Next;
	UINT16 PID;		// Pan ID
	UINT16 Addr;		// Network Address
	UINT8 Channel;
	UINT8  MaxDeny;		// limited deny, try denier if no suitable node found: prevent nobody join into the network
} DENIER_LIST_T;
#endif // #if USE_DENIER_LIST

typedef struct _timer_list_ {
	struct _timer_list_ *Next;
	UINT32 period_count;		// the time period in milliseconds
	UINT32 expire_time;		// expire time of tick_count, in 1 millisecond unit.
	UINT8 overflow:1;		// TRUE if the expire_time is after tick_count overflow.
	UINT8 expired:1;		// TRUE if the time expired.
	UINT8 close_timer_flag:1;	// TRUE to close timer entry after time expired
	UINT8 timer_fn_busy:1;		// TRUE if timer function 'fn' is running
	void (*fn)(void *param);	// function called after time expired
	void *fn_param;			// paramater passing to fn().
} TIMER;

struct _child_list_{
	struct _child_list_ *Next;
	UINT8 MacAddr[8]; // MAC address
	UINT16 NetworkAddress;
	//union _capability_information_field_ Cap; // Capability information
	UINT8 Cap;
	UINT8 Available:1;
	UINT8 rx_polling_flg:1;
	UINT8 rx_polling_switch:1;
	UINT8 tx_failure:1;
	TIMER *tm;
};

struct _broadcast_list_{
	struct _broadcast_list_ *Next;
	UINT16 Address;
	UINT8 Sequence;
};


struct _data_list_{
	struct _data_list_ *Next;
	UINT8 Length;
	UINT8 *Msg;
	UINT16 SrcAddr;
};

#ifdef I_Am_Coordinator
struct _unet_children_table_
{
	struct _unet_children_table_ *Next;
	UINT8 MacAddress[8];
	UINT16 NwkAddress;
	UINT16 ParentAddress;
#if DEBUG_FUNC
	UINT8 rx_rssi_query_number;
#endif
};
#endif

typedef struct _unet_system_parameters_{
	// Unet
	UINT16 JoinPID;		// Join pan id
	UINT8 Channel;		// Join channel
	UINT8 MaxChild;		// network Max. children
	UINT8 MaxRouter;	// network Max. router
	UINT8 MaxDepth;		// network Max. depth
	UINT16 JoinParent;	// Join parent address
	UINT16 OrigParent;	// Original parent
	UINT16 RejoinParent;	// Assigned rejoin parent
	UINT16 NwkAddr;		// Network address (short address)
	UINT8 DevDep;		// Device network depth
	UINT8 MacBSN;		// Mac beacon sequence number
	UINT8 MacDSN;		// Mac packet (Data or Command) sequence number
	UINT8 NwkDSN;		// Nwk seq. number

	UINT8 JoinRouterCnt;	// Join router count
	UINT8 JoinEndevCnt;	// Join end device count
        UINT16 NextAddr;	// Next available short address
	UINT16 AddrInc;		// Address increment

	// System
	UNET_SF Flag;			// Unet flag
	MAC_BP BcnPayload;		// Mac beacon payload
	TIMER *TimerTail;		// The tail of timer chain
#if USE_BEACON_LIST
	struct _beacon_list_  *BcnTail; // The tail of beacon list
#endif
#if USE_DENIER_LIST
	DENIER_LIST_T *DenierTail;	// The tail of denier list
#endif // #if USE_DENIER_LIST
	struct _child_list_ *ChildTail; // The tail of child list
//	UINT8 DataCnt;			//Incoming data count
	struct _data_list_ *DataTail;	// The tail of received data list
	struct _broadcast_list_ *BroTail; // The tail of broadcast list
#ifdef I_Am_Coordinator
	struct _unet_children_table_ *ChildTableTail;
#endif
}UNET_SYS;


#define UART_FIFO_SIZE		32	// must be power of 2
typedef struct _uart_fifo {
	UINT8 buffer[UART_FIFO_SIZE];	// used as a ring buffer
	volatile UINT8 front;    	// index of "get data from FIFO"
	volatile UINT8 rear;		// index of "put data to FIFO"
} UARTFIFO;

typedef struct data_t
{
//	UINT8 Header;
	UINT8 Command;
	UINT8 Data[80];
}UARTDATA;

typedef struct _uart_interface_{
	UARTFIFO rx;
	UARTFIFO tx;
	UINT8 tx_busy:1;
	UINT8 FLAG:1;
	UINT8 PTR;
	UINT8 STATE;
	UINT8 LENGTH;
	UINT8 CHECKSUM;
	UINT8 UNET_CMD[120];
}UART_INTERFACE;

typedef struct _poll_{
	struct{
		UINT8 up_poll_flg:1;
		UINT8 down_poll_flg:1;
		UINT8 Rvd:6;
	}flag;
	UINT32 up_poll_counter;
	UINT32 down_poll_counter;
	UINT32 ETA;
}POLL;

#endif // #ifndef _DATA_H_
